package com.fcscs.ecms.dao.hibernate;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.cservice.domain.model.RestaurantView;
import com.fcscs.ecms.dao.ECMSRestaurantDao;

@SuppressWarnings("unchecked")
@Repository("ecmsaRestaurantDao")
public class ECMSRestaurantDaoHibernate implements ECMSRestaurantDao {


	@Autowired
	private ConnectionManager connMgr;
	
	private static DBLoc DBLoc = new Restaurant().getDBLoc();
	
	private static final String FIND_RESTAURANT_LIST = "FROM Restaurant r where r.orgId=:orgId and r.propertyId=:propId ";
	
	private static final String RESTAURNAT_NAME_CNT  = "select count(*) from restaurant where org_id = :orgId and " +
					" property_id = :propId and name = :name and id <> :id and active = true ";
	
	private static final String FIND_RESTAURANT_VIEW="select r.note as note,r.description as description,"+
				"r.business_location as bussinessLocation,r.cuisine_type_id as cuisineTypeId," +
				"r.default_respic_id as photoId,r.name as name,r.id as restaurantId,r.average_spending as averageSpending,r.smoking as smoking," +
				"t.name as cuisineType from fcs_guest.restaurant r join fcs_guest.cuisine_type t on r.cuisine_type_id=t.id " +
				"where r.org_id =:orgId and r.property_id=:propId and r.active = true and t.active=true";
	
	
	@Override
	public Restaurant getRestaurant(long id) {
		return (Restaurant) connMgr.getCurrentSession(DBLoc).load(Restaurant.class, id);
	}

	@Override
	public List<Restaurant> getRestaurantList(long orgId, long propId,boolean isCurrent) {
		
		Query q = connMgr.getCurrentSession(DBLoc).createQuery(FIND_RESTAURANT_LIST);
		q.setLong("orgId", orgId);
		q.setLong("propId", propId);

		return q.list();
	}

	@Override
	public List<Restaurant> getRestaurantList(long orgId, long propId,long versionId) {
		
		Query q = connMgr.getCurrentSession(DBLoc).createQuery(FIND_RESTAURANT_LIST);
		q.setLong("orgId", orgId);
		q.setLong("propId", propId);

		return q.list();
	}

	@Override
	public List<Restaurant> saveRestaurantList(List<Restaurant> restList) {
		Session session  = connMgr.getCurrentSession(DBLoc);
		for(Restaurant rs : restList){
			session.saveOrUpdate(rs);
		}
		return restList;
	}

	@Override
	public Restaurant createRestaurant(Restaurant rest) {
			connMgr.getCurrentSession(DBLoc).save(rest);
		return rest;
	}

	@Override
	public Restaurant saveRestaurant(Restaurant rest) {
			connMgr.getCurrentSession(DBLoc).saveOrUpdate(rest);
		return rest;
	}

	@Override
	public boolean deleteRestaurant(Restaurant rest) {
		String sql = "Update Restaurant sd set sd.active = false where sd.id=:id";
		Query q = connMgr.getCurrentSession(DBLoc).createQuery(sql);
		q.setLong("id", rest.getId());
		
		return (q.executeUpdate()== 1);
	}

	@Override
	public boolean permDeleteRestaurant(Restaurant rest) {
		try {
			connMgr.getCurrentSession(DBLoc).delete(rest);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	@Override
	public boolean isRestaurantTaken(long orgId, long propId, String name,Long id, long versionId) {
	
		Query q  = connMgr.getCurrentSession(DBLoc).createSQLQuery(RESTAURNAT_NAME_CNT);
		q.setString("name", name);
		q.setLong("id", id == null? -1 : id);
		q.setLong("orgId", orgId);
		q.setParameter("propId", propId, Hibernate.LONG);
		
		return ((Number) q.uniqueResult()).intValue() > 0;
	}

	@Override
	public List<RestaurantView> getRestaurantViewList(long orgId, long propId) {
		
		SQLQuery sqlQu =  connMgr.getCurrentSession(DBLoc).createSQLQuery(FIND_RESTAURANT_VIEW);
		sqlQu.setLong("orgId", orgId);
		sqlQu.setParameter("propId", propId, Hibernate.LONG);
		sqlQu.addScalar("note", Hibernate.STRING).
		addScalar("description", Hibernate.STRING).
		addScalar("bussinessLocation", Hibernate.STRING).
		addScalar("cuisineTypeId", Hibernate.LONG).
		addScalar("photoId", Hibernate.LONG).
		addScalar("name", Hibernate.STRING).
		addScalar("restaurantId", Hibernate.LONG).
		addScalar("averageSpending", Hibernate.BIG_DECIMAL).
		addScalar("smoking",Hibernate.BOOLEAN).
		addScalar("cuisineType", Hibernate.STRING);

		return sqlQu.setResultTransformer(Transformers.aliasToBean(RestaurantView.class)).list();
	}
	
}
